package com.woniuxy.redis01.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author author
 * @Create 2023-09-28 11:32
 */
@Configuration
public class RabbitConfiguration {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //声明队列
    @Bean
    public Queue queueorder(){
        return new Queue("queueorder");
    }

    @Bean
    public Queue queueorderdead(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //@Nullable Map<String, Object> arguments
        Map<String, Object> arguments=new HashMap<>();
        //设置消息的等待时间
        arguments.put("x-message-ttl",10000);
        //变成死信以后，进入那个交换机
        arguments.put("x-dead-letter-exchange","exchangeorderdlx");
        //进入死信交换机以后，通过指定的路由，进入死信队列
        arguments.put("x-dead-letter-routing-key","orderdlx");
        return new Queue("queueorderdead",true,false,false,arguments);
    }


    //topisc模式使用topic类型的交换机
    @Bean
    public TopicExchange exchangeorder(){
        return new TopicExchange("exchangeorder");
    }

    @Bean
    public Binding bindingqueueordertoexchangeorder(Queue queueorder, TopicExchange exchangeorder){
        return BindingBuilder.bind(queueorder).to(exchangeorder).with("#.order.#");
    }
    @Bean
    public Binding bindingqueueorderdeadtoexchangeorder(Queue queueorderdead, TopicExchange exchangeorder){
        return BindingBuilder.bind(queueorderdead).to(exchangeorder).with("#.dead.#");
    }


    @Bean
    public Queue queueorderdlx(){
        return new Queue("queueorderdlx");
    }

    @Bean
    public TopicExchange exchangeorderdlx(){
        return new TopicExchange("exchangeorderdlx");
    }

    @Bean
    public Binding bindingqueueorderdlxtoexchangeorderdlx(Queue queueorderdlx, TopicExchange exchangeorderdlx){
        return BindingBuilder.bind(queueorderdlx).to(exchangeorderdlx).with("#.orderdlx.#");
    }


    @Bean
    public Queue queuedelayplugin(){
        return new Queue("queuedelayplugin");
    }

    @Bean
    public TopicExchange exchangedelayplugin(){
        TopicExchange exchangedelayplugin = new TopicExchange("exchangedelayplugin");
        //交换机启用延迟
        exchangedelayplugin.setDelayed(true);
        return exchangedelayplugin;
    }

    @Bean
    public Binding bindingqueuedelayplugintoexchangeorderdlx(Queue queuedelayplugin, TopicExchange exchangedelayplugin){
        return BindingBuilder.bind(queuedelayplugin).to(exchangedelayplugin).with("#.delayplugin.#");
    }


}
